package com.lr.wfxmanager.permission.controller;

import com.lr.wfxmanager.globa.bean.JsonResultVo;
import com.lr.wfxmanager.permission.bean.ParentFunctree;
import com.lr.wfxmanager.permission.service.IUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;


@RequestMapping("user")
@Controller
public class UserController {
    @Resource
    private IUserService iUserService;

    /**
     * 获取对应用户权限
     * @return
     */
    @RequestMapping("funcList")
    @ResponseBody
    public List<ParentFunctree> funcList(){
        List<ParentFunctree> functreeList = iUserService.queryFunctree("admin");
        return functreeList;
    }

    /**
     * 获取角色权限树
     * @return
     */
    @RequestMapping("checkPermission")
    @ResponseBody
    public List<ParentFunctree> permissionList(String roleCode){
        List<ParentFunctree> rolePermission = iUserService.queryRolePermission(roleCode);
        return rolePermission;
    }
    /*
    * 取消授权
    * */
    @ResponseBody
    @RequestMapping("cancel-permission")
    public JsonResultVo cancel(Integer fid, String roleCode, String moduleCode){
        JsonResultVo resultVo = new JsonResultVo();
        try {
            if (moduleCode.length() == 6){//取消的父节点
                iUserService.cancelRoleGrants(moduleCode, roleCode);
                resultVo.setMsg("parent");
            }else{
                //取消子节点
                iUserService.cancelRoleGrant(fid);
            }
            resultVo.setMark(1);
        } catch (Exception e) {
            resultVo.setMark(0);
            e.printStackTrace();
        }
        return resultVo;
    }

    /**
     * 增加权限
     * @param roleCode
     * @param moduleCode
     * @return
     */
    @RequestMapping("add-permission")
    @ResponseBody
    public JsonResultVo addPermission(String roleCode, String moduleCode){
        System.out.println("roleCode: "+roleCode+",moduleCode: "+moduleCode);
        //给指定角色添加指定权限
        JsonResultVo resultVo = new JsonResultVo();
        try {
            if (moduleCode.length() == 6){//添加的父节点
                iUserService.addRoleGrants(moduleCode,roleCode);
                resultVo.setMsg("parent");
            }else {//添加子节点
                iUserService.addRoleGrant(moduleCode,roleCode);
                resultVo.setMsg("child");
            }
            resultVo.setMark(1);
        } catch (Exception e) {
            resultVo.setMark(0);
            e.printStackTrace();
        }
        return resultVo;
    }
    /**
     * 登录
     * @return
     */
    @ResponseBody
    @RequestMapping("login")
    public JsonResultVo login(String username, String password){
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username,password);
        Subject subject = SecurityUtils.getSubject();
        JsonResultVo resultVo = new JsonResultVo();
        try {
            subject.login(usernamePasswordToken);
            resultVo.setMark(1);
        } catch (AuthenticationException e) {
            e.printStackTrace();
            resultVo.setMark(0);
        }
        return resultVo;
    }
}
